package com.xy.tianyu.module.novel.fragment

import androidx.lifecycle.rxLifeScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.LogUtils
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.novel.adapter.CompetitiveRecommendMultipleAdapter
import com.xy.tianyu.module.novel.bean.CompetitiveRecommendBean
import com.xy.tianyu.module.novel.bean.CompetitiveRecommendMultipleItem
import com.xy.tianyu.network.Api
import com.xy.tianyu.network.ApiParams
import kotlinx.android.synthetic.main.fragment_competitive_recommend.*
import rxhttp.RxHttp
import rxhttp.toClass

/**
 * @time 2020/11/3 14:54
 * @class 描述:
 */
class CompetitiveRecommendFragment :LazyLoadBaseFragment() {
    private var competitiveRecommendMultipleItemList = mutableListOf<CompetitiveRecommendMultipleItem>()
    private lateinit var multipleAdapter : CompetitiveRecommendMultipleAdapter
    override fun setContentView(): Int {
        return R.layout.fragment_competitive_recommend
    }

    override fun lazyLoad() {
        rxLifeScope.launch({
            val competitiveRecommendBean = getBookCompetitiveRecommend()
            val dataList = competitiveRecommendBean.data
            if (!dataList.isNullOrEmpty()){
                for ((index,data) in dataList.withIndex()){
                    val multipleItem = when (index){
                        0 -> data?.let {
                            CompetitiveRecommendMultipleItem(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_BANNER,
                                it
                            )
                        }
                        1 -> data?.let {
                            CompetitiveRecommendMultipleItem(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_HIGH_SCORE,
                                it
                            )
                        }
                        2 -> data?.let {
                            CompetitiveRecommendMultipleItem(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_PUBLISHED_MASTERPIECES,
                                it
                            )
                        }
                        3 -> data?.let {
                            CompetitiveRecommendMultipleItem(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_MAN_MASTERPIECES,
                                it
                            )
                        }
                        4 -> data?.let {
                            CompetitiveRecommendMultipleItem(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_WOMAN_MASTERPIECES,
                                it
                            )
                        }
                        5 -> data?.let {
                            CompetitiveRecommendMultipleItem(CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_FAREWELL_TO_THE_BOOK_SHORTAGE,
                                it
                            )
                        }
                        else -> data?.let {
                            CompetitiveRecommendMultipleItem(
                                CompetitiveRecommendMultipleItem.COMPETITIVE_RECOMMEND_GUESS_YOU_LOVE_WATCHING,
                                it
                            )
                        }
                    }
                    multipleItem?.let { competitiveRecommendMultipleItemList.add(it) }

                    ry_competitive.layoutManager = LinearLayoutManager(activity)
                    multipleAdapter = CompetitiveRecommendMultipleAdapter(competitiveRecommendMultipleItemList);
                    ry_competitive.adapter = multipleAdapter
                }
            }
        },{},{},{})

        refresh_novel_competitive.setOnRefreshListener {

        }

        refresh_novel_competitive.setOnLoadMoreListener {
            refresh_novel_competitive.postDelayed(Runnable {
                refresh_novel_competitive.finishLoadMore()
                multipleAdapter.loadMore()
            },2000)
        }
    }

    override fun stopLoad() {

    }

    private suspend fun getBookCompetitiveRecommend():CompetitiveRecommendBean{
        return RxHttp.get(Api.AI_KAN_SHU_FREE_NOVEL_BASE_URL+Api.AI_KAN_SHU_FREE_NOVEL_COMPETITIVE_URL)
            .addAllHeader(ApiParams.getNovelHeaders())
            .add("type",0)
            .toClass<CompetitiveRecommendBean>()
            .await()
    }
}